Fix GetImplementingProperty issue when using interfaces#144
Fix GetImplementingProperty issue when using interfaces#144PhenX merged 6 commits intoEFNext:masterfrom
Conversation
Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
…ions Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
# Conflicts: # src/EntityFrameworkCore.Projectables/Extensions/TypeExtensions.cs
There was a problem hiding this comment.
Pull request overview
This PR fixes issue #140, where GetImplementingProperty throws System.InvalidOperationException ("Sequence contains no matching element") when a class has both a public property and an explicit interface implementation property sharing the same name (e.g., int Id { get; set; } and string IStringId.Id => ...). The fix changes First(...) to FirstOrDefault(...) ?? propertyInfo in TypeExtensions.GetImplementingProperty, so that when the matching property cannot be found in the derived type's properties, the code gracefully falls back to returning the original interface property rather than throwing.
Changes:
- The single-line source fix in
TypeExtensions.GetImplementingPropertyreplacesFirstwithFirstOrDefaultand adds a?? propertyInfofallback to prevent the crash. - New unit tests and helper types added to
TypeExtensionTests.csverifying bothGetImplementingPropertyandGetConcretePropertyreturn the concrete explicit interface implementation (not the interface property) and do not throw. - New functional and generator snapshot tests added to validate end-to-end behavior for the explicit interface implementation scenario.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
src/EntityFrameworkCore.Projectables/Extensions/TypeExtensions.cs |
Core bug fix: changes First to FirstOrDefault with ?? propertyInfo fallback in GetImplementingProperty |
tests/EntityFrameworkCore.Projectables.Tests/Extensions/TypeExtensionTests.cs |
Adds IStringId/ItemWithExplicitInterfaceImplementation helper types and two unit tests for the fix |
tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.cs |
Adds generator-level snapshot test for a projectable property that accesses an interface member via cast |
tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.ExplicitInterfaceImplementation.verified.txt |
Snapshot verifying the generated expression tree for the new generator test |
tests/EntityFrameworkCore.Projectables.FunctionalTests/ExplicitInterfaceImplementationTests.cs |
Adds functional tests exercising Select and Where on a class with an explicit interface implementation |
tests/EntityFrameworkCore.Projectables.FunctionalTests/ExplicitInterfaceImplementation*.verified.txt (multiple) |
Verified SQL snapshots for DotNet 8/9/10 for both functional test methods |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Fixes #140